// import { gifts } from '../assets/constant'
// import type { GiftMap } from '@/utils/types'
// 封装获取用户等级
async function getUserLevel(image: HTMLImageElement|undefined,levelType:string='permissionLevel') {
  let level: number = 0
  if (image) {
    image.src.replace(/user_grade_level_v5_(.{1,2}).png/ig, (match, $1,index,orignMtch) => {
        level = parseInt($1)
        return match
    })
  }
  const permissionLevelData = await chrome.storage.sync.get(levelType)
  return level<permissionLevelData[levelType]
}
// 获取礼物图片映射
// function getGiftMap(giftsMap:GiftMap):GiftMap {
//   const gitfNotes = document.querySelectorAll('.FQSSDctu')
//   const moreGiftNotes = document.querySelectorAll('.IrR7awlj')[0].querySelectorAll('.KYbfRalq')
  
//   const giftImagesSrc = [] as unknown as Array<string>
//   gitfNotes.forEach(item => {
//     giftImagesSrc.push(item.querySelectorAll('img')[0].src)
//   })
//   const moreGiftImagesSrc = [] as unknown as Array<string>
//   moreGiftNotes.forEach(item => {
//     moreGiftImagesSrc.push(item.querySelectorAll('img')[0].src)
//   })

//   const resultArr= [...giftImagesSrc,...moreGiftImagesSrc].map((item,index) => {
//     const key = item.match(/webcast\/.+\.png/gi)
//     // console.log(key![0])
//     return {
//       giftName: gifts[index],
//       key: key?key[0]:'default',
//       src: item
//     }
//   })
//   // 初始化映射数据
//   const initGifts = gifts.map(item => {
//     return {
//       giftName: item,
//       key: 'default',
//       src: ''
//     }
//   })
//   // initGifts.map(item => {
//   //   const res = giftsMap.find(subItem=>subItem.giftName === item.giftName)
//   //   return res?res:item
//   // })
//   const newGifts = initGifts.map(item => {
//     console.log(item.key,'giftsMapItem');
    
//     const matchGift = resultArr.find(gift=>gift.key === item.key)
//     console.log(matchGift?.key,'matchGiftKey');
    
//     console.log((matchGift?matchGift:item).key,'giftsMap.key')
//     return (matchGift?matchGift:item)
//   })
//   console.log(newGifts)
  
//   return newGifts
// }
// 播放语音
function sayMessage(message?:string) {
  // console.log(message);

  /*把数据存储在服务端*/
  // window
  //   .fetch(`http://localhost:3300/?data=${message}`)
  //   .catch((error) => {
  //     console.log("window.fetch error", error);
  //   });

  var say = new window.SpeechSynthesisUtterance(message || "");
  say.volume = 0.6;
  say.voice = window.speechSynthesis
    .getVoices()
    .filter(
      (voice) => voice.name === "Microsoft Kangkang - Chinese (Simplified, PRC)"
    )[0];

  window.speechSynthesis.speak(say);
}
//识别文本区分
function distinguish(message:string) {
  if (
    /(你是什么|这是|这是啥|这个是啥|这是什么|啥玩意|干什么|干啥|啥意思|干嘛)/gi.test(
      message
    )
  ) {
    sayMessage("我是你的人工智能助理");
  }
  if (
    /(没有欢迎|不欢迎|没有我|我呢|欢迎|不读|bug|读我|咋没有)/gi.test(
      message
    )
  ) {
    const index = message.indexOf("：");
    if (index !== -1) {
      sayMessage(
        message.slice(0, index) + "欢迎你"
      );
    }
  }
  if (/(夸|谢谢)/.test(message)) {
    const index = message.indexOf("：");
    if (index !== -1) {
      sayMessage(
        "夸夸你" + message.slice(0, index) + "你说话真好听"
      );
    }
  }
  if (/(卧槽|尼玛|我去|翻墙)/gi.test(message)) {
    const index = message.indexOf("：");
    if (index !== -1) {
      sayMessage(
        message.slice(0, index) + "请文明用语"
      );
    }
  }
  // if (
  //   /(6|牛|厉害|棒|实力|稀缺|真不错|大哥|六|liu|nb|大佬|火)/gi.test(
  //     message
  //   )
  // ) {
  //   sayMessage("谢谢你夸我");
  // }
}
export { 
  getUserLevel,
  // getGiftMap,
  sayMessage,
  distinguish
}